package com.numberone.master.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 * HttpClient请求
 * @author ck
 *
 * @date 2018年3月6日下午6:35:54
 */
public class HttpClientUtil {
	

	/**
     * HTTP GET请求
     * 
     * @param url
     * @param params
     * @param encoding
     * @return
     */
    public static String doGet(String url, String value) {
    	String encoding="utf-8";
        HttpClient httpClient = new DefaultHttpClient();
        String body = null;
        HttpEntity entity = null;
        try {
        	URL strUrl = new URL(url);
        	URI uri = new URI(strUrl.getProtocol(), strUrl.getHost(), strUrl.getPath(), strUrl.getQuery(), null);
            HttpGet httpget = new HttpGet(uri);
            httpget.setHeader("Content-Type","application/json;charset=utf-8");
            /*
             * 设置参数
             */
            httpget.setURI(new URI(httpget.getURI().toString() + "?" + value));
            /*
             * 发送请求
             */
            HttpResponse httpresponse = httpClient.execute(httpget);
            /*
             * 获取返回数据
             */
            entity = httpresponse.getEntity();
            body = EntityUtils.toString(entity, encoding);
       
        } catch (Exception e) {
        	e.printStackTrace();
        } 
        return body;
    }
    
    public static String sendGet(String url)  {  
        // 创建HttpClient实例     
        HttpClient httpclient = new DefaultHttpClient();  
        // 创建Get方法实例     
        HttpGet httpgets = new HttpGet(url);    
        HttpResponse response;
		try {
			response = httpclient.execute(httpgets);
			HttpEntity entity = response.getEntity(); 
	        if (entity != null) {    
	            InputStream instreams = entity.getContent();    
	            String str = convertStreamToString(instreams);  
	            httpgets.abort();    
	            return str;  
	        }  
		} catch (IOException e) {
			e.printStackTrace();
		}    
        return null;  
    }  
      
    public static String convertStreamToString(InputStream is) {      
        StringBuilder sb1 = new StringBuilder();      
        byte[] bytes = new byte[4096];    
        int size = 0;    
          
        try {      
            while ((size = is.read(bytes)) > 0) {    
                String str = new String(bytes, 0, size, "UTF-8");    
                sb1.append(str);    
            }    
        } catch (IOException e) {      
            e.printStackTrace();      
        } finally {      
            try {      
                is.close();      
            } catch (IOException e) {      
               e.printStackTrace();      
            }      
        }      
        return sb1.toString();      
    }
    
    /**
     * post JSON格式请求
     * @param url url地址
     * @param json 参数
     * @return
     */
    public static String doPost(String url, String json) {
    	 String body = null;
         HttpEntity entity1 = null;
         HttpClient httpClient = new DefaultHttpClient();
		try{
			HttpPost post = new HttpPost(url);
			StringEntity entity = new StringEntity(json, "UTF-8");
			post.setEntity(entity);
			post.setHeader("Content-Type", "application/json;charset=utf-8");
			//logger.info("post:" + post);

	        org.apache.http.HttpResponse httpresponse = httpClient.execute(post);
	        
	        entity1 = httpresponse.getEntity();
	        body = EntityUtils.toString(entity1, "UTF-8");
		}catch (Exception ex) {
			ex.printStackTrace();
			return "exception";
		 } finally {
	            try {
	                EntityUtils.consume(entity1);
	            } catch (IOException e) {
	            }
	            httpClient.getConnectionManager().shutdown();
	        }
		return body;
	}
    
    public static void main(String[] args) {
		System.out.println(sendGet("http://183.230.96.66:8087/v2/gprsrealsingle?appid=GWH32AM&ebid=0001000000008&transid=NM8A1HL2018030614262000000001&token=89aae56a4e693b4e0933b7b83c1a91c2aa2cc124d4c15ffcf9e5290db69c6dcd&iccid=898602B4071770340178"));
	}
}
